{% extends 'layout/manage.html' %}
{% load static %}
{% block css %}
    <style>
        .panel-body {
            padding-top: 0px;
            display: flex;
            flex-direction: row;
            justify-content: left;
            align-items: flex-start;
            flex-wrap: wrap;
        }

        .panel-body > .item {
            border-radius: 6px;
            width: 228px;
            border: 1px solid #dddddd;
            margin: 20px 10px;

        }

        .panel-body > .item:hover {
            border: 1px solid #f0ad4e;
        }

        .panel-body > .item > .title {
            height: 104px;
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            border-top-left-radius: 6px;
            border-top-right-radius: 6px;
            font-size: 15px;
            text-decoration: none;
        }

        .panel-body > .item > .info {
            padding: 10px 10px;

            display: flex;
            justify-content: space-between;

            border-bottom-left-radius: 6px;
            border-bottom-right-radius: 6px;
            color: #8c8c8c;

        }

        .panel-body > .item > .info a {
            text-decoration: none;
        }

        .panel-body > .item > .info .fa-star {
            font-size: 18px;
        }


    </style>
{% endblock %}

{% block content %}

    <div class="panel panel-default">
        <div class="panel-heading"><i class="fa fa-list" aria-hidden="true"></i> 我的橱窗</div>

        <div style="display: flex; margin: 20px 20px 20px 20px; justify-content: left">
            <div class="input-group" style="margin-left: 7px;width: 225px">
                <input type="text" class="form-control" id="searchText"/>
                <span class="input-group-addon btn btn-primary" id="btnSearch"><i class="glyphicon glyphicon-search"></i></span>
            </div>
            <a class="btn btn-primary" data-toggle="modal" data-target="#addModal" style="margin-left: 20px">
                <i class="fa fa-plus-circle" aria-hidden="true"></i> 添加商品
            </a>
        </div>

        <div class="panel-body">
            {% for item in goods_dict.my %}
                <div class="item">
                    <a href="{% url 'wiki' good_id=item.Good_ID %}" class="title"
                       style="background-image: linear-gradient(rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url({% static item.image %}); background-size: cover; background-position: 50% 50%; background-repeat: no-repeat">{{ item.name }}</a>
                    <div class="info">
                        <div>
                            <span>库存：{{ item.stock }}</span>
                        </div>
                        <div>
                            <i class="fa fa-star-o" aria-hidden="true"></i>
                            <span>收藏：{{ item.stars }}</span>
                        </div>
                        <div>
                            <i class="fa fa-user-o" aria-hidden="true"></i>
                            <span>浏览：{{ item.views }}</span>
                        </div>
                    </div>
                </div>
            {% endfor %}
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-heading"><i class="fa fa-handshake-o" aria-hidden="true"></i> 库存记录</div>
        <div class="panel-body">
            <div class="container">
                <table class="table">
                    <thead class="thead-light">
                        <tr>
                            <th scope="col">日期</th>
                            <th scope="col">TranID</th>
                            <th scope="col">商品名</th>
                            <th scope="col">数量</th>
                            <th scope="col">操作人</th>
                            <th scope="col">类别</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in goods_dict.log %}
                            <tr>
                                <td>{{ item.time }}</td>
                                <td>{{ item.Tran_ID }}</td>
                                <td>{{ item.good.name }}</td>
                                <td>{{ item.quantity }}</td>
                                <td>{{ item.operator }}</td>
                                <td>{{ item.get_type_display }}</td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>

                <ul class="pagination">
                    <li class="page-item disabled">
                        <a class="page-link" href="#" tabindex="-1">Previous</a>
                    </li>
                    <li class="page-item active"><a class="page-link" href="#">1</a></li>
                    <li class="page-item"><a class="page-link" href="#">2</a></li>
                    <li class="page-item"><a class="page-link" href="#">3</a></li>
                    <li class="page-item">
                        <a class="page-link" href="#">Next</a>
                    </li>
                </ul>

            </div>
        </div>
    </div>


    <!-- Modal -->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">添加商品</h4>
                </div>
                <div class="modal-body">
                    <form id="addForm">
                        {% for field in form %}
                            <div class="form-group">
                                <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                        {% endfor %}
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                    <button id="btnSubmit" type="button" class="btn btn-primary">确 定</button>
                </div>
            </div>
        </div>
    </div>
{% endblock %}


{% block js %}
    <script>
        $(function () {
            bindSubmit();
            bindSearch();
        });

        /*
            创建商品提交
         */
        function bindSubmit() {
            $('#btnSubmit').click(function () {
                var formData = new FormData();
                formData.append('name', $("#id_name").val());
                formData.append('desc', $("#id_desc").val());
                formData.append('stock', $("#id_stock").val());
                formData.append('type', $("#id_type").val());
                formData.append('image', $("input[name='image']")[0].files[0]);
                $.ajax({
                    url: '{% url 'commodity_list' %}',
                    type: 'POST',
                    data: formData,
                    dataType: 'JSON',
                    enctype: "multipart/form-data",
                    async: true,
                    processData: false,
                    contentType: false,
                    beforeSend: function (xhr, settings) {
                        xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
                    },
                    success: function (res) {
                        if (res.status) {
                            //location.href = location.href
                            location.reload();
                        } else {
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0]);
                            });
                        }
                    }
                });
            });
        }

        function bindSearch(){
            $('#btnSearch').click(function () {
                const input = $("#searchText").val();
                if(input == "") {
                    $(".item").show();
                    return;
                }
                $(".item").each(function() {
                    const itemname = $(this).find('a').text();
                    if(itemname.indexOf(input) > -1) {
                        $(this).show();
                    }else{
                        $(this).hide();
                    }
                });
            });
        }


    </script>
{% endblock %}
